using System.Linq;
using Android.Content;
using Com.Xiaomi.Mipush.Sdk;

namespace TestMiPush.Recevier
{
    
    public class SyMessageReceiver: PushMessageReceiver
    {
        private string _mRegId;
        private long _mResultCode = -1;
        private string _mReason;
        private string _mCommand;
        private string _mMessage;
        private string _mTopic;
        private string _mAlias;
        private string _mUserAccount;
        private string _mStartTime;
        private string _mEndTime;

        public override void OnReceivePassThroughMessage(Context context, MiPushMessage message)
        {
            _mMessage = message.Content;
            if (!string.IsNullOrEmpty(message.Topic))
            {
                _mTopic = message.Topic;
            }
            else if (string.IsNullOrEmpty(message.Alias))
            {
                _mAlias = message.Alias;
            }
            else if (string.IsNullOrEmpty(message.UserAccount))
            {
                _mUserAccount = message.UserAccount;
            }
        }

        public override void OnNotificationMessageClicked(Context context, MiPushMessage message)
        {
            _mMessage = message.Content;
            if (!string.IsNullOrEmpty(message.Topic))
            {
                _mTopic = message.Topic;
            }
            else if (string.IsNullOrEmpty(message.Alias))
            {
                _mAlias = message.Alias;
            }
            else if (string.IsNullOrEmpty(message.UserAccount))
            {
                _mUserAccount = message.UserAccount;
            }
        }

        public override void OnNotificationMessageArrived(Context context, MiPushMessage message)
        {
            _mMessage = message.Content;
            if (!string.IsNullOrEmpty(message.Topic))
            {
                _mTopic = message.Topic;
            }
            else if (string.IsNullOrEmpty(message.Alias))
            {
                _mAlias = message.Alias;
            }
            else if (string.IsNullOrEmpty(message.UserAccount))
            {
                _mUserAccount = message.UserAccount;
            }
        }

        public override void OnCommandResult(Context context, MiPushCommandMessage message)
        {
            var command = message.Command;
            var arguments = message.CommandArguments;
            var cmdArg1 = arguments?.ElementAt(0);
            var cmdArg2 = arguments?.ElementAt(1);

            if (MiPushClient.CommandRegister.Equals(command))
            {
                if (message.ResultCode == ErrorCode.Success)
                {
                    _mRegId = cmdArg1;
                }
            }
            else if (MiPushClient.CommandSetAlias.Equals(command))
            {
                if (message.ResultCode == ErrorCode.Success)
                {
                    _mAlias = cmdArg1;
                }
            }
            else if (MiPushClient.CommandUnsetAlias.Equals(command))
            {
                if (message.ResultCode == ErrorCode.Success)
                {
                    _mAlias = cmdArg1;
                }
            }
            else if (MiPushClient.CommandSubscribeTopic.Equals(command))
            {
                if (message.ResultCode == ErrorCode.Success)
                {
                    _mTopic = cmdArg1;
                }
            }
            else if (MiPushClient.CommandUnsubscribeTopic.Equals(command))
            {
                if (message.ResultCode == ErrorCode.Success)
                {
                    _mTopic = cmdArg1;
                }
            }
            else if (MiPushClient.CommandSetAcceptTime.Equals(command))
            {
                if (message.ResultCode == ErrorCode.Success)
                {
                    _mStartTime = cmdArg1;
                    _mEndTime = cmdArg2;
                }
            }
        }

        public override void OnReceiveRegisterResult(Context context, MiPushCommandMessage message)
        {
            var command = message.Command;
            var arguments = message.CommandArguments;
            var cmdArg1 = arguments?.ElementAt(0);
            var cmdArg2 = arguments?.ElementAt(1);
            if (MiPushClient.CommandRegister.Equals(command))
            {
                if (message.ResultCode == ErrorCode.Success)
                {
                    _mRegId = cmdArg1;
                }
            }
        }
    }
}